package com.yanqu.road.server.channelserver.manager.activity.entername.rank;

import com.yanqu.road.pb.cross3.activity.EnterNameProto;

import java.util.Comparator;
import java.util.LinkedList;

public class EnterNameGroupRankModel {

    private int activityId;
    private int conditionType;
    private int matchGroupId;

    LinkedList<EnterNameGroupRank> rankList;

    private EnterNameProto.EnterNameGroupRankListMsg listMsg;

    private Comparator<EnterNameGroupRank> rankSort = new EnterNameGroupRankSort();

    private boolean save;

    public EnterNameGroupRankModel(int activityId, int conditionType, int matchGroupId) {
        this.activityId = activityId;
        this.conditionType = conditionType;
        this.matchGroupId = matchGroupId;
    }

    public EnterNameGroupRankModel(int activityId, int conditionType, int matchGroupId, Comparator<EnterNameGroupRank> rankSort) {
        this.activityId = activityId;
        this.conditionType = conditionType;
        this.matchGroupId = matchGroupId;
        this.rankSort = rankSort;
    }


    public void rankChange(EnterNameGroupRank rank) {
        boolean isChange = false;
        synchronized (this) {
            EnterNameGroupRank old = getMyRankInfo(rank.getGroupId());
            if(null != old){
                if(0 != old.getValue().compareTo(rank.getValue())){
                    old.setLastUpdateTime(rank.getLastUpdateTime());
                    if(rank.getValue().compareTo(old.getValue()) > 0) {
                        old.setValue(rank.getValue());
                    }
                    old.setParam(rank.getParam());
                    isChange = true;
                }
            }else {
                EnterNameGroupRank newRank = rank.clone();
                rankList.add(newRank);
                isChange = true;
            }
            if(isChange) {
                rankList.sort(rankSort);
                initListMsg();
            }
        }
    }

    public EnterNameGroupRank getMyRankInfo(int groupId) {
        synchronized (this) {
            for(EnterNameGroupRank rank : rankList){
                if(rank.getGroupId() == groupId){
                    return rank;
                }
            }
        }
        return null;
    }

    public int getMyRank(int groupId) {
        int rank = -1;
        int index = 0;
        synchronized (this) {
            for(EnterNameGroupRank userRank : rankList){
                if(userRank.getGroupId() == groupId){
                    rank = index + 1;
                    break;
                }
                index++;
            }
        }
        return rank;
    }

    public void initListMsg(){
        EnterNameProto.EnterNameGroupRankListMsg.Builder builder = EnterNameProto.EnterNameGroupRankListMsg.newBuilder();
        LinkedList<EnterNameGroupRank> list = rankList;
        for (int i = 0; i < list.size(); i++) {
            EnterNameGroupRank rank = list.get(i);
            EnterNameProto.EnterNameGroupRankMsg.Builder rankBuilder = EnterNameProto.EnterNameGroupRankMsg.newBuilder();
            rankBuilder.setGroupId(rank.getGroupId());
            rankBuilder.setValue(rank.getValue().toString());
            builder.addGroup(rankBuilder);
        }
        this.listMsg = builder.build();
    }

    public int getActivityId() {
        return activityId;
    }

    public void setActivityId(int activityId) {
        this.activityId = activityId;
    }

    public int getConditionType() {
        return conditionType;
    }

    public void setConditionType(int conditionType) {
        this.conditionType = conditionType;
    }

    public int getMatchGroupId() {
        return matchGroupId;
    }

    public void setMatchGroupId(int matchGroupId) {
        this.matchGroupId = matchGroupId;
    }

    public Comparator<EnterNameGroupRank> getRankSort() {
        return rankSort;
    }

    public void setRankSort(Comparator<EnterNameGroupRank> rankSort) {
        this.rankSort = rankSort;
    }

    public LinkedList<EnterNameGroupRank> getRankList() {
        return rankList;
    }

    public void setRankList(LinkedList<EnterNameGroupRank> rankList) {
        this.rankList = rankList;
    }

    public EnterNameProto.EnterNameGroupRankListMsg getListMsg() {
        return listMsg;
    }

    public boolean isSave() {
        return save;
    }

    public void setSave(boolean save) {
        this.save = save;
    }
}
